/**
 * @file usr_ec11.c
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-08-19
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#include "usr_cfg.h"
#include "usr_ec11.h"

#define SW1_PORT               (GPIO_PORT_A)
#define SW1_PIN                (GPIO_PIN_02)
#define SW2_PORT               (GPIO_PORT_A)
#define SW2_PIN                (GPIO_PIN_04)

#define SW1_EXTINT_CH          (EXTINT_CH02)
#define SW1_INT_SRC            (INT_SRC_PORT_EIRQ2)
#define SW1_INT_IRQn           (INT001_IRQn)
#define SW1_INT_PRIO           (DDL_IRQ_PRIO_DEFAULT)

#define SW2_EXTINT_CH          (EXTINT_CH04)
#define SW2_INT_SRC            (INT_SRC_PORT_EIRQ4)
#define SW2_INT_IRQn           (INT002_IRQn)
#define SW2_INT_PRIO           (DDL_IRQ_PRIO_DEFAULT)

EC11_EVENT_T ec11_event=EC11_EVENT_NULL;
EC11_EVENT_T pre_ec11_event=EC11_EVENT_NULL;
uint16_t ec11_debounce_cnt=0;

/**
 * @brief  SW1 External interrupt Ch.3 callback function
 * @param  None
 * @retval None
 */
// static void EXTINT_SW1_IrqCallback(void) {
//     // DDL_Printf("i2\n");

//     if (SET == EXTINT_GetExtIntStatus(SW1_EXTINT_CH)) {
//         /* clear int request flag */
//         EXTINT_ClearExtIntStatus(SW1_EXTINT_CH);

//         if (GPIO_ReadInputPins(SW2_PORT, SW2_PIN) == PIN_RESET) {
//             ec11_event = EC11_ROTATE_CCW; /*逆时针旋转*/
//         } else {
//             ec11_event = EC11_ROTATE_CW; /*顺时针旋转*/
//         }
//     }

// }

/**
 * @brief  SW1 External interrupt Ch.3 callback function
 * @param  None
 * @retval None
 */
RAM_FUNC static void EXTINT_SW2_IrqCallback(void) {
     

    if (SET == EXTINT_GetExtIntStatus(SW2_EXTINT_CH)) {
        /* clear int request flag */
        EXTINT_ClearExtIntStatus(SW2_EXTINT_CH);

        if (GPIO_ReadInputPins(SW1_PORT, SW1_PIN) == PIN_SET) {
            if(ec11_debounce_cnt && pre_ec11_event == EC11_ROTATE_CW){

            }else{
                ec11_event = EC11_ROTATE_CCW; /*逆时针旋转*/
            }
            
            // DDL_Printf("i3\n");
        } else {
            if(ec11_debounce_cnt && pre_ec11_event == EC11_ROTATE_CCW){

            }else{
                ec11_event = EC11_ROTATE_CW; /*顺时针旋转*/
            }
            // DDL_Printf("i4\n");
        }
        pre_ec11_event = ec11_event;
        ec11_debounce_cnt = EC11_DEBOUNCE_TIME;
    }

}
void usr_ec11_debounce_handle(void){
    if(ec11_debounce_cnt){
        ec11_debounce_cnt--;
        if(ec11_debounce_cnt==0){
            pre_ec11_event=EC11_EVENT_NULL;
        }
    }
}
EC11_EVENT_T get_ec11_roate_state(void){
    EC11_EVENT_T ret = ec11_event;

    ec11_event = EC11_EVENT_NULL;

    return ret;

}
/**
 * @brief  SW1 initialize
 * @param  None
 * @retval None
 */
void ec11_rec_io_init(void) {
    stc_extint_init_t stcExtIntInit;
    stc_gpio_init_t stcGpioInit;
    stc_irq_signin_config_t stcIrqSignConfig;

    /* GPIO config */
    (void)GPIO_StructInit(&stcGpioInit);
    // stcGpioInit.u16ExtInt = PIN_EXTINT_ON;
    stcGpioInit.u16ExtInt = PIN_EXTINT_OFF;
    stcGpioInit.u16PullUp = PIN_PU_ON;
    (void)GPIO_Init(SW1_PORT, SW1_PIN, &stcGpioInit);

    stcGpioInit.u16ExtInt = PIN_EXTINT_ON;
    stcGpioInit.u16PinDir = PIN_DIR_IN;
    stcGpioInit.u16PullUp = PIN_PU_ON;
    (void)GPIO_Init(SW2_PORT, SW2_PIN, &stcGpioInit);

    // /* ExtInt config */
    // (void)EXTINT_StructInit(&stcExtIntInit);
    // stcExtIntInit.u32Filter = EXTINT_FILTER_ON;
    // stcExtIntInit.u32FilterClock = EXTINT_FCLK_DIV1;
    // stcExtIntInit.u32Edge = EXTINT_TRIG_FALLING;
    // (void)EXTINT_Init(SW1_EXTINT_CH, &stcExtIntInit);

    // /* IRQ sign-in */
    // stcIrqSignConfig.enIntSrc = SW1_INT_SRC;
    // stcIrqSignConfig.enIRQn = SW1_INT_IRQn;
    // stcIrqSignConfig.pfnCallback = &EXTINT_SW1_IrqCallback;
    // (void)INTC_IrqSignIn(&stcIrqSignConfig);

    // /* NVIC config */
    // NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
    // NVIC_SetPriority(stcIrqSignConfig.enIRQn, SW1_INT_PRIO);
    // NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);

    /* ExtInt config */
    (void)EXTINT_StructInit(&stcExtIntInit);
    stcExtIntInit.u32Filter = EXTINT_FILTER_ON;
    stcExtIntInit.u32FilterClock = EXTINT_FCLK_DIV1;
    stcExtIntInit.u32Edge = EXTINT_TRIG_FALLING;
    (void)EXTINT_Init(SW2_EXTINT_CH, &stcExtIntInit);

    /* IRQ sign-in */
    stcIrqSignConfig.enIntSrc = SW2_INT_SRC;
    stcIrqSignConfig.enIRQn = SW2_INT_IRQn;
    stcIrqSignConfig.pfnCallback = &EXTINT_SW2_IrqCallback;
    (void)INTC_IrqSignIn(&stcIrqSignConfig);

    /* NVIC config */
    NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
    NVIC_SetPriority(stcIrqSignConfig.enIRQn, SW2_INT_PRIO);
    NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
}
